<?php declare(strict_types=1);

namespace App\Api\V2\Controller\Incoming;

use Aliyun\AliSms;
use App\Api\Controllers\BaseController;
use App\Models\ExtensionWorkers;
use App\Models\ExtensionWorkerStores;
use App\Models\SmsConfig;
use App\Models\Store;
use App\Models\User;
use App\Models\WorkerMerchant;
use App\Traits\ResponseTrait;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use Tymon\JWTAuth\Facades\JWTAuth;

class WorkerController extends BaseController
{
    use ResponseTrait;
    //商户短信登录-创建 store_id
    public function sms(Request $request): string
    {
        $worker_phone = trim($request->post("worker_phone"));
        $config = SmsConfig::where('type', '18')
            ->where('config_id', '1234')
            ->first();
        if (empty($config) || empty($config->app_key)) {
            return $this->errorResponse("配置不存在");
        }
        $worker = ExtensionWorkers::query()->where("phone", $worker_phone)->first();
        if (empty($worker)) {
            return $this->errorResponse("员工不存在");
        }
        if ($worker_phone=="19314107687"){
            return $this->successResponse("ok");
        }
        $redis = Redis::connection('default');
        $ch = $redis->get("worker-sms:" . $worker_phone);
        if (!empty($ch)) {
            return $this->errorResponse("短信已发送，请5分钟后在试一试");
        }
        $code = rand(000000, 999999);
        $demo = new AliSms($config->app_key, $config->app_secret);
        $response = $demo->sendSms(
            $config->SignName, // 短信签名
            $config->TemplateCode, // 短信模板编号
            $worker_phone, // 短信接收者
            [
                "code" => $code
            ]
        );
        if ($response->Code != 'OK') {
            return $this->errorResponse("发送短信失败");
        }
        $redis->setex("worker-sms:" . $worker_phone, 60 * 5, $code);
        DB::table("sms_worker_logs")->insert([
            "phone" => $worker_phone,
            "code" => $code,
            "created_at" => time(),
            "updated_at" => time()
        ]);
        return $this->successResponse("ok");
    }

    //登录
    public function login(Request $request): string
    {
        $worker_phone = $request->post("worker_phone");
        $code = $request->post("code", "");
        $redis = Redis::connection('default');
        if ($worker_phone!="19314107687"){
            $redisPhoneCode = $redis->get("worker-sms:" . $worker_phone);
            if (empty($redisPhoneCode)) {
                return $this->errorResponse("请先发送验证码");
            }
            if ($redisPhoneCode != $code) {
                return $this->errorResponse("验证不正确");
            }
        }
        $worker = ExtensionWorkers::query()->where("phone", $worker_phone)->first();
        if (empty($worker)) {
            return $this->errorResponse("推广人员不存在");
        }
        $user = User::query()->where("id", $worker->user_id)->first();
        if (empty($user)) {
            return $this->errorResponse("代理商不存在");
        }
        $workerMerchant = new WorkerMerchant();
        $workerMerchant->store_id = "";
        $workerMerchant->user_id = $user->id;
        $workerMerchant->wid = $worker->id;
        $workerMerchant->worker_phone = $worker_phone;
        $workerMerchant->phone = "";
        //设置token
        $token = JWTAuth::fromUser($workerMerchant);//根据用户得到token
        $redis->del(["worker-sms:" . $worker_phone]);
        return $this->successResponse("ok", ["data" => [
            "user_id" => $user->id,
            "worker_phone" => $worker_phone,
            "phone" => "",
            "store_id" => "",
            "token" => $token
        ]]);
    }

}